package kr.saeildamil.androidforstudy2.ch33;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;

public class SoundPoolTest extends Activity {
	SoundPool mPool;
	int dingdong;
	int stream;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_soundpooltest);
		
		mPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		dingdong = mPool.load(this, R.raw.dingdong, 1);
	}
	
	SoundPool.OnLoadCompleteListener mListener = new OnLoadCompleteListener() {
		
		@Override
		public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
			if(status == 0){
				stream = soundPool.play(sampleId, 1, 1, 0, 0, 1);
			}
		}
	};
	
	public void mOnClick(View v){
		MediaPlayer mPlayer;
		switch (v.getId()) {
		case R.id.play1: // 일반 재생;
			mPool.play(dingdong, 1, 1, 0, 0, 1);
			break;
		case R.id.play2: // 볼륨의 절반, 재생;
			mPool.play(dingdong, 0.5f, 0.5f, 0, 0, 1);
			break;
		case R.id.play3: // 3회 반복 재생;
			mPool.play(dingdong, 1, 1, 0, 2, 1);
			break;
		case R.id.play4: // 1/2배속 재생;
			mPool.play(dingdong, 1, 1, 0, 0, 0.5f);
			break;
		case R.id.play5: // 직후 재생;
			dingdong = mPool.load(this, R.raw.goodtime, 1);
			mPool.play(dingdong, 1, 1, 0, 0, 1);
			break;
		case R.id.play6: // 로딩 완료 후 재생;
			mPool.setOnLoadCompleteListener(mListener);
			mPool.load(this, R.raw.goodtime, 1);
			break;
		case R.id.play7: // 잠시 멈춤;
			mPool.autoPause();
			break;
		case R.id.play8: // 멈춘 위치에서 재생;
			mPool.autoResume();
			break;
		}
	}
}
